package com.bjjnts.util;

import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SimpleVerifyUtil {

    /**
     *
     * @param token
     * @param class_id
     * @param course_id
     * @param unit_id
     */
    public static void simpleSmart(String token,String class_id, String course_id, String unit_id) {
        String url = "https://apif.bjjnts.cn/supervises/smart-new";
        Map<String, String> headers = new HashMap<>();
        headers.put("Accept", "application/json, text/plain, */*");
        headers.put("Accept-Encoding", "gzip, deflate, br");
        headers.put("Accept-Language", "zh-CN,zh;q=0.9");
        headers.put("Connection", "keep-alive");
        headers.put("Content-Type", "application/json");
        headers.put("Host", "apif.bjjnts.cn");
        headers.put("Origin", "https://www.bjjnts.cn");
        headers.put("sec-ch-ua", "\"Google Chrome\";v=\"93\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"93\"");
        headers.put("sec-ch-sec-ch-ua-mobile", "?0");
        headers.put("sec-ch-ua-platform", "\"Windows\"");
        headers.put("Sec-Fetch-Dest", "empty");
        headers.put("Sec-Fetch-Mode", "cors");
        headers.put("Sec-Fetch-Site", "same-site");
        headers.put("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36");
        headers.put("X-Client-Type", "pc");
        headers.put("Referer", "https://www.bjjnts.cn/study/video?class_id=" + class_id + "&course_id=" + course_id + "&unit_id=" + unit_id);
        headers.put("Authorization", "Bearer " + token);

        JSONObject params = new JSONObject();
        params.set("course_id", course_id);
        params.set("unit_id", unit_id);
        params.set("class_id", class_id);

        HttpRequest.post(url).headerMap(headers, true).body(JSONUtil.toJsonStr(params)).execute().body();
    }

    /**
     * 简单人脸识别
     * @param userName
     * @param token
     * @param class_id
     * @param course_id
     * @param unit_id
     */
    public static void simpleFace(String userName, String token,String class_id, String course_id, String unit_id) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Accept", "application/json, text/plain, */*");
        headers.put("Accept-Encoding", "gzip, deflate, br");
        headers.put("Accept-Language", "zh-CN,zh;q=0.9");
        headers.put("Connection", "keep-alive");
        headers.put("Content-Type", "application/json");
        headers.put("Host", "apif.bjjnts.cn");
        headers.put("Origin", "https://www.bjjnts.cn");
        headers.put("sec-ch-ua", "\"Google Chrome\";v=\"93\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"93\"");
        headers.put("sec-ch-sec-ch-ua-mobile", "?0");
        headers.put("sec-ch-ua-platform", "\"Windows\"");
        headers.put("Sec-Fetch-Dest", "empty");
        headers.put("Sec-Fetch-Mode", "cors");
        headers.put("Sec-Fetch-Site", "same-site");
        headers.put("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36");
        headers.put("X-Client-Type", "pc");
        headers.put("Referer", "https://www.bjjnts.cn/study/video?class_id=" + class_id + "&course_id=" + course_id
                + "&unit_id=" + unit_id);
        headers.put("Authorization", "Bearer " + token);
        String url = "https://apif.bjjnts.cn/supervises";
        JSONObject json = new JSONObject();
        json.set("baseImage", getFace(userName));
        json.set("course_id", course_id);
        json.set("unit_id", unit_id);
        json.set("class_id", class_id);
        String body = HttpRequest.post(url).headerMap(headers, true).body(JSONUtil.toJsonStr(json)).execute().body();
        log.info(body);
    }

    private static String getFace(String userName) {
        Font font = new Font(Font.DIALOG, Font.PLAIN, 10);
        URL faceURL = ResourceUtil.getResource("face" + File.separator + userName+ ".jpg");
        int content = RandomUtil.randomInt(0, 9);
        int x = RandomUtil.randomInt(0, 287);
        int y = RandomUtil.randomInt(0, 287);
        Img img = Img.from(FileUtil.file(faceURL)).pressText(content + "", Color.white,
                font, x, y, 1.0F);
        return ImgUtil.toBase64(img.getImg(), "jpg");
    }
}
